package com.webermagem.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GerarInserts {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

//		gerarInsertsSintDiag();

		gerarInsertsDiagInter();

//		gerarInsertsSintomas();

//		gerarInsertsSintDiag();

	}

	private static void gerarInsertsSintomas() {

		try {

			BufferedReader in = new BufferedReader(new FileReader("dbconfig/arquivo_sintomas"));

			String str = null;

			int cont = 0;
			int contTipoSintoma = 1;
			int contSintoma = 1;

			List<String> listaTipo = new ArrayList<String>();
			List<String> listaSintoma = new ArrayList<String>();

			while (in.ready()) {

				str = in.readLine();

				if ("".equals(str.trim())) {

					cont = 0;
					continue;

				}

				cont++;

				if (cont == 1) {

					listaTipo.add("INSERT INTO webermagem.\"tb_tipo_sintoma\" (id, descricao) VALUES (" + contTipoSintoma + ", '" + str.trim() + "');");
					contTipoSintoma++;

				} else {

					listaSintoma.add("INSERT INTO webermagem.\"tb_sintomas\" (id, descricao, id_tipo) VALUES (" + contSintoma + ", '" + str.replaceAll("==", "").trim() + "', " + (contTipoSintoma-1) + ");");
					contSintoma++;

				}

			}

			System.out.println("---------------");
			System.out.println("---------------");
			for (String string : listaTipo) {
				System.out.println(string);
			}
			System.out.println("---------------");
			System.out.println("---------------");
			for (String string : listaSintoma) {
				System.out.println(string);
			}

			in.close();

		} catch (IOException e) {e.printStackTrace();}

	}

	private static void gerarInsertsSintDiag() {

		Map<String, Integer> listaSintomas = listaSintomas();
		Map<String, Integer> listaDiagnosticos = listaDiagnosticos();
		List<Integer> listaIdSintomas = new ArrayList<Integer>();
		List<Integer> listaIdDiag = new ArrayList<Integer>();
		
		try {

			BufferedReader in = new BufferedReader(new FileReader("dbconfig/arquivo_sint_diag"));

			String str = null;
			String strDiag = null;
			int cont = 1;
			
			while (in.ready()) {

				str = in.readLine().trim();
				strDiag = str.replaceAll("==", "").trim();

				if ("".equals(str)) {
					
					for (Integer idSintoma : listaIdSintomas) {
						for (Integer idDiag : listaIdDiag) {
							System.out.println("INSERT INTO webermagem.\"tb_sint_diag\" (id, id_sintoma, id_diagnostico) VALUES (" + cont + "," + idSintoma + "," + idDiag + ");");
							cont++;
						}
					}
					listaIdSintomas = new ArrayList<Integer>();
					listaIdDiag = new ArrayList<Integer>();
					continue;
				}

				if (!str.contains("==")) {

					listaIdSintomas.add(listaSintomas.get(str));
					

				} else {
					
					listaIdDiag.add(listaDiagnosticos.get(strDiag));

				}

			}

			in.close();

		} catch (IOException e) {e.printStackTrace();}

	}

	private static Map<String, Integer> listaSintomas() {

		Map<String, Integer> listaSintomas = new HashMap<String, Integer>();

		try {

			BufferedReader in = new BufferedReader(new FileReader("dbconfig/arquivo_sintomas_insert"));

			Integer idSintoma = null;
			String str = null;
			String strForm = null;

			while (in.ready()) {

				str = in.readLine().substring(69);
				
				String[] strSplit = str.split(",");
				idSintoma = Integer.parseInt(strSplit[0].substring(1));
				strForm = strSplit[1].trim().replaceAll("'", "");
				strForm = strForm.substring(0, strForm.length());
				
				listaSintomas.put(strForm,idSintoma);

			}

			in.close();

		} catch (IOException e) {e.printStackTrace();}

		return listaSintomas;

	}
	
	private static Map<String, Integer> listaDiagnosticos() {

		Map<String, Integer> listaSintomas = new HashMap<String, Integer>();

		try {

			BufferedReader in = new BufferedReader(new FileReader("dbconfig/arquivo_diagnosticos_insert"));
			
			Integer idSintoma = null;
			String str = null;
			String strForm = null;

			while (in.ready()) {

				str = in.readLine().substring(73);
				
				String[] strSplit = str.split(",");
				idSintoma = Integer.parseInt(strSplit[0].substring(1));
				strForm = strSplit[1].trim().replaceAll("'", "");
				strForm = strForm.substring(0, strForm.length()-2);
				
				listaSintomas.put(strForm, idSintoma);


			}

			in.close();

		} catch (IOException e) {e.printStackTrace();}

		return listaSintomas;

	}

	private static void gerarInsertsDiagInter() {

		try {

			BufferedReader in = new BufferedReader(new FileReader("dbconfig/arquivo_diag_inter"));

			String str = null;

			int cont = 0;
			int contDiag = 1;
			int contInter = 1;
			int contDiagInter = 1;

			List<String> listaDiagnostico = new ArrayList<String>();
			List<String> listaInter = new ArrayList<String>();
			List<String> listaDiagInter = new ArrayList<String>();

			while (in.ready()) {

				str = in.readLine();

				if ("".equals(str.trim())) {

					cont = 0;
					continue;

				}

				cont++;

				if (cont == 1) {

					listaDiagnostico.add("INSERT INTO webermagem.\"tb_diagnosticos\" (id, descricao, id_tipo) VALUES (" + contDiag + ", '" + str + "');");
					contDiag++;

				} else {

					listaInter.add("INSERT INTO webermagem.\"tb_intervencoes\" (id, descricao, id_tipo) VALUES (" + contInter + ", '" + str + "');");
					listaDiagInter.add("INSERT INTO webermagem.\"tb_diag_interv\" (id_diagnostico, id_intervencao) VALUES ("+ (contDiag-1) + ", " + contInter + ");");
					contInter++;

				}

			}

			System.out.println("---------------");
			System.out.println("---------------");
			for (String string : listaDiagnostico) {
				System.out.println(string);
			}
			System.out.println("---------------");
			System.out.println("---------------");
			for (String string : listaInter) {
				System.out.println(string);
			}
			System.out.println("---------------");
			System.out.println("---------------");
			for (String string : listaDiagInter) {
				System.out.println(string);
			}

			in.close();

		} catch (IOException e) {e.printStackTrace();}


	}

}
